﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace Daily
{
    public static class ConfigManager
    {
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        private static Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location );
        public static string getConfigInfo(string key)
        {
            if (config == null || config.AppSettings.Settings[key] == null)
            {
                return "";
            }
            else
            {
                return config.AppSettings.Settings[key].Value;
            }
        }

        /// <summary>
        /// 设置配置信息
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns>成功</returns>
        public static bool setConfigInfo(string key, string value)
        {
            try
            {
                if (config == null) return false;
                if (config.AppSettings.Settings[key] == null)
                {
                    KeyValueConfigurationElement kvce = new KeyValueConfigurationElement(key, value);
                    config.AppSettings.Settings.Add(kvce);
                }
                else
                {
                    config.AppSettings.Settings[key].Value = value;
                }
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }
}
